package org.jrpc.client3;

import com.sun.security.ntlm.Client;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoop;
import org.jrpc.base.IClient;

import java.util.concurrent.TimeUnit;

/**
 * Created by chenyuncong on 2019/4/11.
 * 重新连接服务端
 */

public class ConnectionListener implements ChannelFutureListener {

    private IClient iClient;

    public ConnectionListener(IClient iClient) {
        this.iClient = iClient;
    }
    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        System.out.println("=========重新连接服务器，准备=========="+channelFuture.isSuccess());
        if (!channelFuture.isSuccess()) {
            System.out.println("Reconnect");
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule((Runnable) () -> {
                System.out.println("=========重新连接服务器==========");
                try {
                    iClient.createBootstrap(new Bootstrap(),loop);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, 5L, TimeUnit.SECONDS);
        }
    }
}